require(['./config'], () => {
    require(['url', 'template', 'header','jquery','bootstrap'], (url, template) => {
      class Index {
        constructor () {
          this.indexList()
          this.render()
          this.scrollSearch()
          this.search()
          this.goTop()
        }
        indexList () {
          $.get( "http://quan.lukou.com/api/tabs?sa=", resp => {
            if (resp.code === 200) {
              resp.data.list = resp.data.list.slice(1)
              const  { list }  = resp.data
              $('#indexList').html(template('indexListTemplate', { list }))
            }
          })
        }
        render () {
          $.get(`${url.xmyxBaseUrl}/tab/1`, {start: 0}, resp => {
            if (resp.code === 200) {
              let { list } = resp.data.items
              list = list.filter(shop => shop.title)
              $('#goodsList').html(template('goodsListTemplate', { list }))
            }
          })
        }
        // 监听页面滚动，并控制头部搜索框以及实时渲染数据
        scrollSearch(){
          let TF = true
          let num = 1
          $(document).scroll(function() {
            var winH = $(window).height()
            var pageH = $(document.body).height(); 
            var scroH = $(window).scrollTop(); //滚动条top 
            var aa = (pageH-winH-scroH)/winH;
            if(scroH >= 200){
              $('#topSearch').css('top','0px')
              $('#goTop').fadeIn()
            }else{
              $('#topSearch').css('top','-52px')
              $('#goTop').fadeOut()
            }
            if(aa <= 0.4){
              if(TF && num < 11){
                TF = false
                $.get(`${url.xmyxBaseUrl}/tab/1`, {start: num *20}, resp => {
                  if (resp.code === 200) {
                    let { list } = resp.data.items
                    list = list.filter(shop => shop.title)
                    $(`#goodsList${num+1}`).html(template(`goodsListTemplate${num+1}`, { list }))
                  }
                })
                num++
                setTimeout(() => {TF = true},1000)
              }
            } 
          })
        }
        search(){
          $('#searchTxt').on('keydown',function(e){
            if(e.which == 13){
              $('#searchBtn').trigger('click')
            }
          })
          $('#searchBtn').on('click',function(){
            const word = $('#searchTxt').val()
            window.open(`/html/search.html?word=${word}`,'_blank')
          })
          $('#searchTxt2').on('keydown',function(e){
            if(e.which == 13){
              $('#searchBtn').trigger('click')
            }
          })
          $('#searchBtn2').on('click',function(){
            const word = $('#searchTxt').val()
            window.open(`/html/search.html?word=${word}`,'_blank')
          })
        }
        goTop(){
          $('#goTop').hide()
          $('#goTop').on('click',function(){
            var scroH = $(document).scrollTop()
            console.log(scroH)
            var goTop = setInterval(function(){
              $(document).scrollTop(scroH -= 20)
              if(scroH == 0) clearInterval(goTop)
            },1)
          })
        }
      }
      new Index()
    })
  })